package com.etlmaster.executor.steps.func;


import com.etlmaster.executor.StepMeta;
import com.etlmaster.executor.annotation.StepInstruction;
import com.etlmaster.executor.utils.Shell;
import com.etlmaster.executor.utils.StepState;

/**
 * 
 * @author fengl
 */
@StepInstruction("localCmd")
public class ExecLocalCmdStep extends FuncStep {
	
	private ExecLocalCmdStepMeta meta;
	
	
	public ExecLocalCmdStep(StepMeta meta){
		super(meta);
		this.meta = (ExecLocalCmdStepMeta)meta;
	}
	
	@Override
	protected int handle() {
		int flag=StepState.RUNNING_SUCCESSED;
		String localCmd=meta.getLocalCmd();
		if(localCmd==null)
			return StepState.RUNNING_FAILED;
		try {
			//String result = Shell.execCommand(localCmd);
			String[] commands=localCmd.split(" ");
			Shell.ShellCommandExecutor executor= new Shell.ShellCommandExecutor(commands);
			
			String result=executor.runCmdLog(localCmd);
			int rtnFlag=executor.getExitCode();
			if(rtnFlag==0){
				flag = StepState.RUNNING_SUCCESSED;
				
			}else{
				meta.log("ERROR","本地命令({}) 返回状态值({})不为0.请检查", localCmd,rtnFlag+"");
				flag = StepState.RUNNING_FAILED;
			}
			meta.log("INFO",executor.getOutput());
			meta.getStepState().getReturnValue().put("stepReturn", result);
		} catch (Exception e) {
			flag = StepState.RUNNING_FAILED;
			meta.log("ERROR","本地命令({})执行错误 ({})", localCmd,e.getMessage());
		}
		

		
		return flag;
	}

	@Override
	public boolean stop() {
		// TODO Auto-generated method stub
		return false;
	}

}
